﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS.Data;
using ECare.BUS;
using ECare.Client.Services;
using ECare.BUS.Workers;

namespace ECare.Client.controls
{
    public partial class ucWizardResources : ucWizardInfo
    {
        public ucWizardResources()
        {
            InitializeComponent();
            this.IsLoaded = false;
        }


        public plan_detail_step_info currentstep { get; set; }

        public bool IsLoaded { get; set; }


        public override  void LoadInformation()
        {
            this.lbLoading.Visible = true;
            this.lblNo.Text = (this.Index + 1).ToString();
            this.lblTitle.Text = string.Format(" 物料 {0}", ResourcesHelper.GetResourceName(currentstep.resource_id));

            this.lblResourceNumber.Text = ResourcesHelper.GetResourceName(currentstep.resource_id);
            this.lblNeeded.Text = currentstep.resource_need.ToString();//需求数量
            //    this.lblResource.Text = string.Format("物料  {0}，  需要数量 {1}   KG", 
            //        ResourcesHelper.GetResourceName(currentstep.resource_id), currentstep.resource_need);
            // this.lblCarInfo.Text = string.Format("{0}   号车. ", currentstep.resource_car_number);


            this.lblCarNumber.Text = currentstep.resource_car_number.ToString();

            if (this.currentstep.resource_ischangecar)
            {
                this.lblNextCar.Text = string.Format("下一步将换车");
            }
            else
            {
                this.lblNextCar.Text = string.Empty;
            }

        //    var halfbarcode = await LoadInfo();
            string halfbarcode = string.Empty;
            if (string.IsNullOrEmpty(halfbarcode))
            {
                this.lblHalf.Text = string.Format(this.lblHalf.Text, "否");
            }
            else
            {
                this.lblHalf.Text = string.Format(this.lblHalf.Text, halfbarcode);
            }


           // Total = await GetStorage();


            //this.lblMessage.Text = string.Format(this.lblMessage.Text, Total);

            //if (Total > this.currentstep.resource_need)
            //    this.lblMessage.ForeColor = Color.Green;
            //else
            //{
            //    this.lblMessage.ForeColor = Color.Red;
            //}

            var imagebuffer=ResourcesHelper.GetResourceImage(currentstep.resource_id);
            if(imagebuffer!=null)
                this.pic.BackgroundImage= ImageHelper.GetImageFromBase64(imagebuffer);



            this.lbLoading.Visible = false;

            this.LoadStepStatus();
        }

        public void LoadStepStatus()
        {
            if (this.currentstep.plan_detail_step_status == 1)
            {
                this.lblStatus.Text = string.Format("与 {0} 完成", this.currentstep.plan_detail_end_date);
                this.lblStatus.ForeColor = Color.Green;
            }
            else
            {
                this.lblStatus.Text = "未完成...";
                this.lblStatus.ForeColor = Color.Red;
            }
        }


        public decimal Total { get; set; }

     //   private async Task<string> LoadInfo()
     //   {
     //   //    return await Task.Run(() => ResourcesHelper.GetHalfBarcodeInProducing(this.currentstep.resource_id));
     //   }

     //   private async Task<decimal> GetStorage()
     //   {
     ////       return await Task.Run(() => ResourcesHelper.GetResourceTotalInProducing(this.currentstep.resource_id));
     //   }

 

        public override bool ValidateInformation()
        {
            this.lblCurrent.Text = this.txtWeight.Value.ToString("F2");if (currentstep.plan_detail_step_status ==1)
                return true;

            if (this.txtBarcode.Text.Trim() != ResourcesHelper.GetResourceCode(currentstep.resource_id))
            {
                this.errorProvider1.SetError(this.txtBarcode, "条形码扫描不一致.");
                return false;
            }
            else
            {
                this.errorProvider1.SetError(this.txtBarcode, string.Empty);
            }

            if (this.lblCurrent.Text != currentstep.resource_need.ToString("F2"))
            {
                this.errorProvider1.SetError(this.lblCurrent, "数量不对！");
                return false;
            }
            else
            {
                this.errorProvider1.SetError(this.lblCurrent,string.Empty);
            }
            return true;
        }

        public override bool Execute()
        {
            if (this.currentstep.plan_detail_step_status == 1)
            {
                //如果已经完成了，则直接进入下一步，不进行任何操作.
                if (this.Finished != null)
                    this.Finished(true);
                return true;
            }


            //执行出库操作。
            var worker = new ComputeResourceInProducingWorker();
            worker.Currentstep = this.currentstep;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
            this.lbLoading.Visible = true;

            return true;
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.lbLoading.Visible = false;
            if (Finished != null)
            {
                this.currentstep.plan_detail_step_status = (bool)e.Result ? 1 : 0;

                this.LoadStepStatus();

                this.Finished((bool)e.Result);
            }

        }

        public event StepFinishedHandler Finished;

        private void txtWeight_ValueChanged(object sender, EventArgs e)
        {
            this.lblCurrent.Text = this.txtWeight.Value.ToString("F2");
        }
    }
}
